<template>
  <!-- 表单基础信息 -->
  <div>
    <title-name v-if="isShowTitle" :title="isOwn ?'自有计量设备信息' : '项目分包计量设备信息'"></title-name>
    <custom-form ref="ruleForm" :form="form" :formList="formList" :edit-model="''" :is-emit="false"
                 :formComponents="$refs" @selectOrg="selectOrg">
      <template #mdId>
        <a-select v-model="form.mdId" placeholder="请选择" :disabled="editModel == 1 || editModel == 0"
                  @select="mdIdValSelect">
          <a-select-option v-for="item in deviceNameData" :key="item.id">
            {{ item.mdName }}
          </a-select-option>
        </a-select>
      </template>
      <template #mdModelId>
        <a-select v-model="form.mdModelId" :disabled="editModel == 1 || editModel == 0"
                  placeholder="请选择" allowClear>
          <a-select-option v-for="item in getMdModelData" :key="item.id">
            {{ item.specificationModel }}
          </a-select-option>
        </a-select>
      </template>
    </custom-form>
  </div>
</template>

<script>
import { ownFormList } from './data.js' // data
import api from '@/api/quality/ownEquipmentLedger' // api
import equipmentNameApi from '@/api/quality/equipmentNameManagement' // api
export default {
  name: 'equipmentBaseForm',
  props: {
    form: Object,
    // 是否显示title
    isShowTitle: {
      type: Boolean,
      default: true
    },
    isOwn: {
      type: Boolean,
      default: true
    },
    //是否展示进场/退场时间
    isHasTime: {
      type: Boolean,
      default: false
    },
    //新增2编辑1查看0
    editModel: {
      type: [String, Number],
      default() {
        return 0
      }
    },
    //所属单位[计量设备管理]模块默认需要权限控制，
    //待办业务中直接显示label，不需要权限控制
    isAccessControl: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {
      api,
      equipmentNameApi,//计量设备名称管理API
      deviceNameData: [],//设备名称list
    }
  },
  computed: {
    formList() {
      return ownFormList(this.editModel, this.isHasTime, this.isAccessControl, this.form)
    },
    getMdModelData() {
      if (this.deviceNameData.length && Object.keys(this.form).length && this.form.mdId) {
        let _obj = this.deviceNameData.find((item) => item.id == this.form.mdId);
        return _obj && Object.keys(_obj).length ? _obj.list : [];
      } else {
        return [];
      }
    }
  },
  created() {
    this.getDeviceNameList();
  },
  methods: {
    //选中所属单位
    async selectOrg(val) {
      if (this.editModel == 2) {
        this.$emit("hanleOrgSelect", null)
        if (val && val.length) {
          let res = await this.api.generateMdCode({ branchCompanyId: val[0].orgId });
          if (res.code == '0') {
            this.$emit("hanleOrgSelect", res.data)
          }
        }
      }
    },
    //获取可用的设备列表
    async getDeviceNameList() {
      this.deviceNameData = [];
      const requestParameters = Object.assign({}, {
        pageNum: 1,
        pageSize: 0
      }, { param: { status: 0 } })
      let res = await this.equipmentNameApi.list(requestParameters)
      if (res.code === '0') {
        this.deviceNameData = res.data.list || []
      }
    },
    //选中值改变
    mdIdValSelect() {
      this.$emit("clearData", ['mdModelId']);
    }
  }
}
</script>

<style lang="less" scoped>
</style>